package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.AppointmentQueryObject;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Autowired
    private StatementMapper statementMapper;
    @Override
    public TablePageInfo<Appointment> query(AppointmentQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        appointment.setCreateTime(new Date());
        appointmentMapper.insert(appointment);

    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        Appointment appointment1 = this.get(appointment.getId());

        if (!Appointment.STATUS_APPOINTMENT.equals(appointment1.getStatus())) {
            throw new BusinessException("要在预约状态才可以编辑");
        }
        appointmentMapper.updateByPrimaryKey(appointment);


    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void arrivalOn(Long id) {
        Appointment appointment = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("在预约状态才能到店操作");
        }

        appointmentMapper.arrivalOn(id, Appointment.STATUS_ARRIVAL);
    }

    @Override
    public void arrivalOff(Long id) {
        Appointment appointment = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("在预约状态才能取消操作");
        }

        appointmentMapper.arrivalOff(id, Appointment.STATUS_CANCEL);
    }

    @Override
    public Long statementPay(Long id) {

        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        Statement statement = statementMapper.selectByappointment(appointment.getId());

        if (statement == null) {
            statement = new Statement();
            statement.setActualArrivalTime(new Date());
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setLicensePlate(appointment.getLicensePlate());
            statement.setCarSeries(appointment.getCarSeries());
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setServiceType(Long.valueOf(appointment.getServiceType()));
            statement.setAppointmentId(appointment.getId());
            statementMapper.insert(statement);
        }

        appointmentMapper.chenkE(id,Appointment.STATUS_SETTLE);
        return statement.getId();

    }
}
